export function phoneType() {
	if (window) {
		const ua = window.navigator.userAgent.toLocaleLowerCase();
		const izIOS = /iphone|ipad|ipod/.test(ua);
		const isAndroid = /android/.test(ua);
		if (isAndroid) {
			return 'android'
		} else if (izIOS) {
			return 'ios'
		}
	}
}

/**
 * @param {Object} num 当前数
 * @param {Object} total 总数
 */
export function GetPercent(num, total) {
	/// <summary>
	/// 求百分比
	/// </summary>
	/// <param name="num">当前数</param>
	/// <param name="total">总数</param>
	num = parseFloat(num);
	total = parseFloat(total);
	if (isNaN(num) || isNaN(total)) {
		return "-";
	}
	return total <= 0 ? 0 : (Math.round(num / total * 10000) / 100.00);
}

/**
 * @param {Object} col 颜色
 */
export function colorRgb(col) {
	// 16进制颜色值的正则
	var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
	// 把颜色值变成小写
	var color = col.toLowerCase();
	if (reg.test(color)) {
		// 如果只有三位的值，需变成六位，如：#fff => #ffffff
		if (color.length === 4) {
			var colorNew = "#";
			for (var i = 1; i < 4; i += 1) {
				colorNew += color.slice(i, i + 1).concat(color.slice(i, i + 1));
			}
			color = colorNew;
		}
		// 处理六位的颜色值，转为RGB
		var colorChange = [];
		for (var i = 1; i < 7; i += 2) {
			colorChange.push(parseInt("0x" + color.slice(i, i + 2)));
		}
		return colorChange.join(",");
	} else {
		return color;
	}	
}

function _chooseImage(t = 'all') {
	return new Promise((resolve, reject) => {
		uni.getSetting({
			success: (res) => {
				console.log(res, '666')
				if (t == 'album') {
					if (res.authSetting['scope.album'] != false) {
						resolve()
					} else {
						uni.showModal({
							title: '温馨提示',
							content: '为了更好的体验小程序,请授权相册哦~',
							showCancel: false,
							success: () => {
								uni.openSetting()
							}
						})
					}
				} else if (t == 'camera') {
					if (res.authSetting['scope.camera'] != false) {
						resolve()
					} else {
						uni.showModal({
							title: '温馨提示',
							content: '为了更好的体验小程序,请授权相册哦~',
							showCancel: false,
							success: () => {
								uni.openSetting()
							}
						})
					}
				} else {
					if (res.authSetting['scope.album'] != false && res.authSetting[
							'scope.camera'] != false) {
						resolve()
					} else {
						uni.showModal({
							title: '温馨提示',
							content: '为了更好的体验小程序,请授权相册哦~',
							showCancel: false,
							success: () => {
								uni.openSetting()
							}
						})
					}
				}
			}
		})
	})
}


/**
 * 节流函数
 * @param {Function} fn 需要执行的方法，因this指向问题，建议不使用箭头函数，
 * @param {Number} interval 间隔时间，默认值500
 * **/
export function throttle(fn, interval = 500) {
	let timer = 0;
	return function(...arg) {
		let _this = this;
		if (timer) return;
		fn.call(_this, ...arg);
		timer = setTimeout(() => {
			timer = 0;
		}, interval);
	};
};